import { RegisterComponent } from "@feng3d/ecs";
import { Plane } from "../../thirdparty/three/exports";
import { PlaneHelper } from "../../thirdparty/three/imports";
import { Component3D } from "../core/Component3D";

declare module "@feng3d/ecs"
{
    interface ComponentMap { PlaneHelper3D: PlaneHelper3D; }
}

/**
 * 包装`three`中`PlaneHelper`。
 */
@RegisterComponent({ name: 'PlaneHelper3D' })
export class PlaneHelper3D extends Component3D
{
    _helper: PlaneHelper;

    get plane()
    {
        if (this._helper)
        {
            this._plane = this._helper.plane;
        }

        return this._plane;
    }
    set plane(v)
    {
        if (this._helper)
        {
            this._helper.plane = v;
        }
        this._plane = v;
    }
    private _plane: Plane;

    get size()
    {
        if (this._helper)
        {
            this._size = this._helper.size;
        }

        return this._size;
    }
    set size(v)
    {
        if (this._helper)
        {
            this._helper.size = v;
        }
        this._size = v;
    }
    private _size: number;

    /**
     * 初始化
     */
    // eslint-disable-next-line accessor-pairs
    set hex(v: number)
    {
        console.assert(!this._helper);
        this._hex = v;
    }
    private _hex: number;

    init()
    {
        super.init();
        this._helper = new PlaneHelper(this.plane, this.size, this._hex);
        this._entity._group.add(this._helper);
    }

    destroy(): void
    {
        this._entity._group.remove(this._helper);

        this._helper.dispose();
        this._helper = null;

        super.destroy();
    }
}
